/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ServerService.java
 *
 * Created on Sep 6, 2011, 11:06:54 AM
 */
package cw.ui;

import com.jtattoo.plaf.aero.AeroLookAndFeel;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

/**
 *
 * @author VinhTC
 */
public class ServerService extends javax.swing.JFrame {

    SV sv = new SV();
    boolean isPause = false;

    /** Creates new form ServerService */
    public ServerService() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jtxtServerAdd = new javax.swing.JTextField();
        jtxtPort = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jbtnStart = new javax.swing.JButton();
        jbtnPause = new javax.swing.JButton();
        jbtnStop = new javax.swing.JButton();
        jbtnHintConfig = new javax.swing.JButton();
        jlblServerRunning = new javax.swing.JLabel();
        jlblServerPaused = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Server configuration");
        setResizable(false);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosed(java.awt.event.WindowEvent evt) {
                formWindowClosed(evt);
            }
        });

        jPanel1.setName("jPanel1"); // NOI18N

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image/InstallServer.png"))); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Server configuration"));
        jPanel2.setName("jPanel2"); // NOI18N

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel2.setText("Server address: ");
        jLabel2.setName("jLabel2"); // NOI18N

        jtxtServerAdd.setName("jtxtServerAdd"); // NOI18N

        jtxtPort.setName("jtxtPort"); // NOI18N

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel3.setText("Port: ");
        jLabel3.setName("jLabel3"); // NOI18N

        jLabel4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image/Settings.png"))); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N

        jbtnStart.setForeground(new java.awt.Color(0, 51, 51));
        jbtnStart.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image/player_play.png"))); // NOI18N
        jbtnStart.setText("Start");
        jbtnStart.setName("jbtnStart"); // NOI18N
        jbtnStart.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnStartActionPerformed(evt);
            }
        });

        jbtnPause.setForeground(new java.awt.Color(0, 51, 51));
        jbtnPause.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image/pause.png"))); // NOI18N
        jbtnPause.setText("Pause");
        jbtnPause.setName("jbtnPause"); // NOI18N
        jbtnPause.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnPauseActionPerformed(evt);
            }
        });

        jbtnStop.setForeground(new java.awt.Color(0, 51, 51));
        jbtnStop.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image/cancel_1.png"))); // NOI18N
        jbtnStop.setText("Close");
        jbtnStop.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        jbtnStop.setName("jbtnStop"); // NOI18N
        jbtnStop.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnStopActionPerformed(evt);
            }
        });

        jbtnHintConfig.setForeground(new java.awt.Color(0, 51, 51));
        jbtnHintConfig.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image/export16.png"))); // NOI18N
        jbtnHintConfig.setText("Hint config");
        jbtnHintConfig.setName("jbtnHintConfig"); // NOI18N
        jbtnHintConfig.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnHintConfigActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel2Layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addComponent(jbtnHintConfig)
                        .addGap(18, 18, 18)
                        .addComponent(jbtnStart, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(29, 29, 29)
                        .addComponent(jbtnPause))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel2Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jtxtServerAdd, javax.swing.GroupLayout.PREFERRED_SIZE, 203, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jtxtPort, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 19, Short.MAX_VALUE)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(44, 44, 44))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jbtnStop, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(jtxtServerAdd, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3)
                            .addComponent(jtxtPort, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(jLabel4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 36, Short.MAX_VALUE)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jbtnHintConfig)
                    .addComponent(jbtnStop)
                    .addComponent(jbtnPause, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jbtnStart))
                .addGap(19, 19, 19))
        );

        jlblServerRunning.setVisible(false);
        jlblServerRunning.setFont(new java.awt.Font("Tahoma", 1, 11));
        jlblServerRunning.setForeground(new java.awt.Color(0, 153, 51));
        jlblServerRunning.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image/awaiting.gif"))); // NOI18N
        jlblServerRunning.setText("Server is running");
        jlblServerRunning.setHorizontalTextPosition(javax.swing.SwingConstants.LEFT);
        jlblServerRunning.setName("jlblServerRunning"); // NOI18N

        jlblServerPaused.setVisible(false);
        jlblServerPaused.setFont(new java.awt.Font("Tahoma", 1, 11));
        jlblServerPaused.setForeground(new java.awt.Color(204, 0, 0));
        jlblServerPaused.setText("Server paused...");
        jlblServerPaused.setName("jlblServerPaused"); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLabel1)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(28, 28, 28)
                .addComponent(jlblServerPaused, javax.swing.GroupLayout.PREFERRED_SIZE, 155, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 155, Short.MAX_VALUE)
                .addComponent(jlblServerRunning, javax.swing.GroupLayout.PREFERRED_SIZE, 136, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(26, 26, 26))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jlblServerRunning, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jlblServerPaused, javax.swing.GroupLayout.PREFERRED_SIZE, 18, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(29, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        java.awt.Dimension dialogSize = getSize();
        setLocation((screenSize.width-dialogSize.width)/2,(screenSize.height-dialogSize.height)/2);
    }// </editor-fold>//GEN-END:initComponents

    private void jbtnStopActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnStopActionPerformed
        int index = JOptionPane.showConfirmDialog(jbtnStop, "Are you sure want to exit?", "Confirm for exit", JOptionPane.YES_NO_OPTION);
        if (index == 0) {
            System.exit(0);
        }
    }//GEN-LAST:event_jbtnStopActionPerformed

    private void jbtnPauseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnPauseActionPerformed
        int index = JOptionPane.showConfirmDialog(jbtnStop, "Are you sure want to pause server?", "Confirm for pause server", JOptionPane.YES_NO_OPTION);
        if (index == 0) {
            if (!isPause) {
                isPause = true;
                jbtnPause.setText("Resume");
                jlblServerPaused.setVisible(true);
                jlblServerRunning.setVisible(false);
                sv.suspend();
            } else {
                jlblServerPaused.setVisible(false);
                jlblServerRunning.setVisible(true);
                isPause = false;
                jbtnPause.setText("Pause");
                sv.resume();
            }
        }
    }//GEN-LAST:event_jbtnPauseActionPerformed

    private void jbtnStartActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnStartActionPerformed
        if (true) {
            sv.start();
        }

        //        try {
//            boolean flag;
//            String ip = jtxtServerAdd.getText().trim();
//            String port = jtxtPort.getText().trim();
//            cw.server.Server server = new cw.server.Server();
//            server.setIP(ip);
//            server.setPort(port);
//            String[] args = null;
//            server.main(args);
//
//            if (server.getStatus()) {
//                JOptionPane.showMessageDialog(null, "Server is running...");
//                jbtnStart.setEnabled(false);
//                flag = true;
//                jlblServerRunning.setVisible(true);
//            } else {
//                JOptionPane.showMessageDialog(null, "Server can not start...");
//                flag = false;
//                jlblServerRunning.setVisible(false);
//                jbtnStart.setEnabled(false);
//            }
//        } catch (Exception ex) {
//            System.out.println(ex.getMessage());
//        }
    }//GEN-LAST:event_jbtnStartActionPerformed

    private void formWindowClosed(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosed
        SV sv = new SV();
        sv.stop();
    }//GEN-LAST:event_formWindowClosed

    private void jbtnHintConfigActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnHintConfigActionPerformed
        try {
            jtxtServerAdd.setText(InetAddress.getLocalHost().getHostAddress().toString());
            jtxtPort.setText("1200");
        } catch (UnknownHostException ex) {
            Logger.getLogger(ServerService.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_jbtnHintConfigActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                try {
                    UIManager.setLookAndFeel(new AeroLookAndFeel());
                    new ServerService().setVisible(true);
                } catch (UnsupportedLookAndFeelException ex) {
                    Logger.getLogger(ServerService.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JButton jbtnHintConfig;
    private javax.swing.JButton jbtnPause;
    private javax.swing.JButton jbtnStart;
    private javax.swing.JButton jbtnStop;
    private javax.swing.JLabel jlblServerPaused;
    private javax.swing.JLabel jlblServerRunning;
    private javax.swing.JTextField jtxtPort;
    private javax.swing.JTextField jtxtServerAdd;
    // End of variables declaration//GEN-END:variables

    class SV extends Thread {

        @Override
        @SuppressWarnings("static-access")
        public void run() {
            if (config()) {
            } else {
            }
        }

        @SuppressWarnings("static-access")
        public boolean config() {
            boolean flag = false;
            try {
                String ip = jtxtServerAdd.getText().trim();
                String port = jtxtPort.getText().trim();
                cw.server.Server server = new cw.server.Server();
                server.setIP(ip);
                server.setPort(port);
                String[] args = null;
                server.main(args);

                if (server.getStatus()) {
                    JOptionPane.showMessageDialog(null, "Server is running...");
                    jbtnStart.setEnabled(false);
                    flag = true;
                    jlblServerRunning.setVisible(true);
                } else {
                    JOptionPane.showMessageDialog(null, "Server can not start...");
                    flag = false;
                    jlblServerRunning.setVisible(false);
                    jbtnStart.setEnabled(false);
                }
            } catch (Exception ex) {
                System.out.println(ex.getMessage());
            }
            return flag;
        }
    }
}
